package com.tc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tc.domain.Medical_service_items;
import com.tc.dto.Medical_service_itemsQueryDTO;
import com.tc.mapper.Medical_service_itemsMapper;
import com.tc.service.IMedical_service_itemsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tc.vo.Medical_service_itemsVO;
import com.tc.vo.PageResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jcd
 * @since 2025-07-09
 */
@Service
public class Medical_service_itemsServiceImpl extends ServiceImpl<Medical_service_itemsMapper, Medical_service_items> implements IMedical_service_itemsService {

    /**
     * 分页查询医疗服务项目信息
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @Override
    public PageResultVO<Medical_service_itemsVO> pageQuery(Medical_service_itemsQueryDTO queryDTO) {
        // 构建分页查询对象
        Page<Medical_service_items> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        
        // 构建查询条件
        LambdaQueryWrapper<Medical_service_items> queryWrapper = new LambdaQueryWrapper<>();
        
        // 显式添加未删除过滤条件
        queryWrapper.eq(Medical_service_items::getDeleted, 0);
        
        // 添加查询条件
        if (StringUtils.hasText(queryDTO.getFinancialCategory())) {
            queryWrapper.eq(Medical_service_items::getFinancialCategory, queryDTO.getFinancialCategory());
        }
        if (StringUtils.hasText(queryDTO.getProjectCode())) {
            queryWrapper.eq(Medical_service_items::getProjectCode, queryDTO.getProjectCode());
        }
        if (StringUtils.hasText(queryDTO.getNationalCode())) {
            queryWrapper.eq(Medical_service_items::getNationalCode, queryDTO.getNationalCode());
        }
        if (StringUtils.hasText(queryDTO.getProjectName())) {
            queryWrapper.like(Medical_service_items::getProjectName, queryDTO.getProjectName());
        }
        
        // 价格区间查询
        if (queryDTO.getMinPrice() != null) {
            queryWrapper.ge(Medical_service_items::getPrice, queryDTO.getMinPrice());
        }
        if (queryDTO.getMaxPrice() != null) {
            queryWrapper.le(Medical_service_items::getPrice, queryDTO.getMaxPrice());
        }
        
        // 执行分页查询
        IPage<Medical_service_items> pageResult = this.page(page, queryWrapper);
        
        // 转换records
        List<Medical_service_itemsVO> voList = new ArrayList<>();
        for (Medical_service_items item : pageResult.getRecords()) {
            Medical_service_itemsVO vo = new Medical_service_itemsVO();
            BeanUtils.copyProperties(item, vo);
            voList.add(vo);
        }
        
        // 使用通用PageResultVO
        return PageResultVO.convert(pageResult, voList);
    }
}
